<?php 

abstract class EW_Form_Base extends Zend_Form {
    
    /**
     * Counter for instances, using to create an unique form id.
     * 
     * @var int
     */
    private static $_countInstance = 0;
    
    /**
     * 
     * Unique Form Id
     * 
     * @var string
     */
    protected $_formId = NULL;
    
    /**
     * 
     * @var EW_Core_Controller
     * @access protected
     */
    protected $_controller = NULL;
    
	/**
     * Default Constructor
     * 
     * @param array $options
     * @param Ew_Core_Controller $controller
     * 
     * @return EW_Form_Base
     */
    function __construct($options, Ew_Core_Controller &$controller) {
        $this->_controller = $controller;
        
        self::$_countInstance++;
        $this->_formId = self::$_countInstance;
        
        parent::__construct($options);
    }
    //==========================================================================================
    
    /**
     * Initialize Form
     * 
     * This is the new method for initialization. 
     * This is the substitute for Zend_Form::init().
     * 
     * @return unknown_type
     */
    abstract protected function _initialize();
    
    /**
     * Saving Form in Database.
     * 
     * Build the new db record & save.
     * 
     * @return void
     * @access protected
     */
    abstract protected function _save();
    
    /**
     * Initialize decorators
     * 
     * @return void
     */
    abstract protected function _initDecorators();
    
    /**
     * Before Save Callback
     * 
     * @return void
     */
    abstract protected function _beforeSave();
    
     /**
     * After Save Callback
     * 
     * @return void
     */
    abstract protected function _afterSave();
    
    /**
     * Final Zend_Form::init()  overriding
     * 
     * Finalizing original Zend initialization method with adding extra functionality
     * to detect when the form is posted and automaticaly save the form fields in db.
     * 
     * @return void
     */
    final function init() {
        /*
         * Add Form Id as hidden field
         */
        $this->addElement('hidden', 'formId', array('value'=>$this->_formId));
        
        $this->_initialize();
        
        if ($this->isSubmit()) {
            $request = $this->_controller->getRequest();
            if ( $this->isValid($request->getPost())) {
                $this->_save();
                
            }
        }
        
        $this->_initDecorators();
    }
    //==========================================================================================
    
    protected function isSubmit() {
        if( (($this->getMethod()===self::METHOD_POST) && isset($_POST['formId']) && ($_POST['formId']==$this->_formId)) ||
             (($this->getMethod()===self::METHOD_GET) && isset($_GET['formId']) && ($_GET['formId']==$this->_formId)) ) {
             
             return TRUE;
        }
         
        return FALSE;
    }
    //==========================================================================================
    
}